#pragma once
#include "Globals.h"
#include "WorldPosition.h"
#include "Font.h"
#include "Camera.h"
#include "Light.h"
#include "Material.h"
#include "Texture.h"
#include "Mesh.h"


namespace Renderer
{

	class Graphics
	{
	private:
		Font font;

	    bool drawFPS;
		short FPS;
		float fpsTimer;
		short frameCount;

	protected:
		D3DDEVTYPE devType;
		DWORD requestedVP;
		IDirect3D9 * d3dObject;
		D3DPRESENT_PARAMETERS d3dPP;
		IDirect3DDevice9 * d3dDevice;

		ID3DXSprite * sprite;
		

		HINSTANCE hInst;
		HWND hWnd;
		D3DDISPLAYMODE	d3dDisplayMode;
		bool isHAL;
		bool zBufferEnabled;
		bool windowed;
		int	width, height;
		char BPP;
		unsigned char ambientRed;
		unsigned char ambientGreen;
		unsigned char ambientBlue;



	public:
		Graphics(HINSTANCE hInst, HWND hWnd, D3DDEVTYPE devType, DWORD requestedVP);
		virtual ~Graphics(void);

		IDirect3D9* GetDirect3DCOM() const;
		IDirect3DDevice9* GetDeviceCOM() const;
		ID3DXSprite*	GetSpriteCOM() const;

		virtual bool CheckDeviceCaps();
		virtual void OnLostDevice();
		virtual void OnResetDevice();
		virtual bool IsDeviceLost();
		virtual void Update(float dt);
		virtual bool Draw();

		virtual bool BeginScene();
		virtual bool EndScene();

		virtual bool BeginSprite();
		virtual bool EndSprite();

		virtual bool InitD3D();
		virtual bool Shutdown();

		virtual void Resize(RECT & clientRect);
		virtual bool SetMode(HWND hWnd, bool windowed = true, bool useZBuffer = false, int width = 0, int height = 0, char BPP = 0);

		bool Clear(long color, float zBuffer = 1.0f);
		bool ClearDisplay(long color = 0);
		bool ClearZBuffer(float zBuffer = 1.0f);

		int GetBackBufferWidth() const;
		int GetBackBufferHeight() const;
		char GetBPP() const;
		bool GetHAL() const;
		bool GetZBuffer() const;

		UINT GetNumDisplayModes(D3DFORMAT format);
		bool GetDisplayModeInfo(UINT num, D3DDISPLAYMODE * mode, D3DFORMAT format);

		char GetFormatBPP(D3DFORMAT format);

		//Helper function to test if d3dDevice supports specified format
		bool CheckFormat(D3DFORMAT format, bool windowed, bool HAL);

		virtual void FullScreen(bool enable);

		int Windowed(void) const;

		void SetBackBufferWidth(WORD width);
		void SetBackBufferHeight(WORD height);

		bool SetPerspective(float FOV = D3DX_PI / 4.0f, float aspect = 1.3333f, float near = 1.0f, float far = 10000.0f);
		D3DXMATRIX GetPersective();

		bool SetWorldPosition(WorldPosition * worldPos);
		bool SetCamera(Camera* camera);
		bool SetLight(UINT num, Light* light);
		bool SetMaterial(Material* material);
		bool SetTexture(USHORT num, Texture* texture);

		bool SetAmbientLight(BYTE red, BYTE green, BYTE blue);
		bool GetAmbientLight(BYTE* red, BYTE* green, BYTE* blue);

		bool EnableLight(DWORD num, BOOL enable = TRUE);
		bool EnableLighting(BOOL enable = TRUE);
		bool EnableZBuffer(BOOL enable = TRUE);
		bool EnableAlphaBlending(BOOL enable = TRUE, DWORD source = D3DBLEND_SRCALPHA, DWORD dest = D3DBLEND_INVSRCALPHA);
		bool EnableAlphaTesting(BOOL enable = TRUE);


		void DrawFPS();

	};


}